/**
 * 
 */
package com.study.singleton;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.omg.Dynamic.Parameter;


/**
 * @author Administrator
 * @date 2019年6月25日
 */
//使用单例模式，读取两个节点的xml文件，实例化唯一一个选择区域的对象Map
//使用 dom4j 读取 xml
public class SelectCodeUtil {

  //1、私有静态变量
  private static SelectCodeUtil selectCodeUtil;
  private Map<String,List<String>> selectMap;//业务对象
  private Document document;
  
  //2、私有构造方法
  private SelectCodeUtil(String filePath){
      List<String> listPosition = new ArrayList<String>();
      List<String> listDistrict = new ArrayList<String>();
      selectMap = new HashMap<String, List<String>>();
      
      try {
          //载入文件
          SAXReader saxReader = new SAXReader();
          document = saxReader.read(new File(filePath));
          //获取根节点
          Element root = document.getRootElement();
          Iterator<Element> eleBrands = root.elementIterator();
          //读取节点
          while(eleBrands.hasNext()){
              Element brand = (Element)eleBrands.next();
              String key = brand.attributeValue("name");
              if(key.equals("position")){
                  
                  Iterator<Element> eleTypes = brand.elementIterator();
                  while(eleTypes.hasNext()){
                      Element type = (Element)eleTypes.next();
                      String parameter = type.attributeValue("key") +"---"+ type.attributeValue("value");
                      listPosition.add(parameter);
                  }
                  //把取到的值放进Map
                  selectMap.put(key, listPosition);
                  
              }
              if(key.equals("district")){
                  
                  Iterator<Element> eleTypes = brand.elementIterator();
                  while(eleTypes.hasNext()){
                      Element type = (Element)eleTypes.next();
                      String parameter = type.attributeValue("key") +"---"+ type.attributeValue("value");
                      listDistrict.add(parameter);
                  }
                  //把取到的值放进Map
                  selectMap.put(key, listDistrict);
                  
              }
      
          }
      } catch (DocumentException e) {
          e.printStackTrace();
      }
  }
  
  //3、公有静态方法，获取实例对象 
  public static synchronized SelectCodeUtil getInstance(String filePath){
      if(null == selectCodeUtil){
          selectCodeUtil = new SelectCodeUtil(filePath);
      }
      return selectCodeUtil;
  }
  
  //4、公有业务方法
  public Map<String, List<String>> getSelectMap(){
      return  selectMap;
  }
  
  
  //测试    
  public static void main(String[] args) {
      //xml文件绝对路径
      String filePath = new File("src/main/resources/selectCode.xml").getAbsolutePath();
      
      Map<String, List<String>> map = SelectCodeUtil.getInstance(filePath).getSelectMap();
      
      //遍历输出
      Set<String> keys = map.keySet();
      for(String key : keys){
          System.out.println("节点  "+key);
          List<String> list = (List<String>)map.get(key);
          for(String p:list){
              System.out.println("\t"+p);
          }
      }
  }

}
